/*
  Copyright (c) 2010 Xynilex Project

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR #include "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
 */

// System Headers
#include <map>
#include <fstream>
#include <string>

// Boost Headers
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>

// Xynilex Headers
#include "Common/CommandManager.h"
#include "Common/Types.h"

using Xynilex::String;
using Xynilex::CommandManager;

CommandManager::CommandManager()
{
}

CommandManager::~CommandManager()
{
}

void CommandManager::registerCommand(const String& name, CommandHandler* handler)
{
  this->mCommandHandlers.insert(this->mCommandHandlers.lower_bound(name), std::pair<String, CommandHandler*>(name, handler));
}

void CommandManager::unregisterCommand(const String& name)
{
  this->mCommandHandlers.erase(this->mCommandHandlers.find(name));
}

void CommandManager::unregisterCommand(const String& name, CommandHandler* handler)
{
  CommandHandlerMap::iterator start = this->mCommandHandlers.lower_bound(name);
  CommandHandlerMap::iterator end = this->mCommandHandlers.upper_bound(name);

  for(CommandHandlerMap::iterator it = start; it != end; it++)
  {
    if(it->second == handler)
    {
      this->mCommandHandlers.erase(it);
      break;
    }
  }
}

bool CommandManager::runCommand(Player* sender, const String& raw)
{
  StringList args;
  boost::algorithm::split_regex(args, raw, boost::regex("[ \t]"));

  if(args.size() < 1)
  {
    return false;
  }

  String command = args.at(0);
  args.erase(args.begin());

  return this->runCommand(sender, command, args);
}

bool CommandManager::runCommand(Player* sender, const String& name, const StringList& args)
{
  CommandHandlerMap::iterator start = this->mCommandHandlers.lower_bound(name);
  CommandHandlerMap::iterator end = this->mCommandHandlers.upper_bound(name);

  bool handled = false;

  for(CommandHandlerMap::iterator it = start; it != end; it++)
  {
    if(it->second->handle(sender, name, args))
    {
      handled = true;
      break;
    }
  }

  return handled;
}

bool CommandManager::loadFromFile(const std::string& file, Player* sender)
{
  std::ifstream fh;
  fh.open(file.c_str());

  if(!fh.is_open())
  {
    return false;
  }

  String line;

  while(std::getline(fh, line))
  {
    boost::trim(line);

    StringList parts;
    boost::split(parts, line, boost::is_any_of("#"));

    boost::trim(parts[0]);
    String command = parts[0];

    this->runCommand(sender, command);
  }

  return true;
}
